package edu.pitt.dbmi.odie.gapp.gwt.client.user;

import java.util.List;
import java.util.Map;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;

import edu.pitt.dbmi.odie.gapp.gwt.client.ODIE_TabbedNavigator;

public class ODIE_LoginPanel extends VLayout {

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final ODIE_LoginServiceAsync odieLoginService = GWT
			.create(ODIE_LoginService.class);

	private ODIE_LoginInfo loginInfo = null;

	private Anchor signInLink = new Anchor("Sign In");
	private Anchor signOutLink = new Anchor("Sign Out");

	private Layout signInLayout = null;
	private Layout signOutLayout = null;
	
	private ODIE_TabbedNavigator tabbedNavigator ;

	public ODIE_LoginPanel(ODIE_TabbedNavigator tabbedNavigator) {
		
		this.tabbedNavigator = tabbedNavigator ;
		
		// Initialize layout 
		setMembersMargin(15);
		setLayoutMargin(50);
		setHeight("400");

		// Get the login information
		getLoginInfo() ;
	}
	
	private void getLoginInfo() {
		String clientParametersXml = xmlizeClientParameters() ;
		Log.debug(clientParametersXml) ;
		odieLoginService.login(GWT.getHostPageBaseURL(),
				clientParametersXml,
				new AsyncCallback<ODIE_LoginInfo>() {
					public void onFailure(Throwable error) {
					}

					public void onSuccess(ODIE_LoginInfo result) {
						loginInfo = result;
						if (loginInfo.isLoggedIn()) {
							loadLogout();
						} else {
							loadLogin();
						}
					}
				});
	}
	
	private String xmlizeClientParameters() {
		Log.debug(Window.Location.getHref()) ;
		Log.debug(Window.Location.getHost()) ;
		Log.debug(Window.Location.getHostName()) ;
		Log.debug(Window.Location.getPath()) ;
		Log.debug(Window.Location.getPort()) ;
		Log.debug(Window.Location.getProtocol()) ;
		Log.debug(Window.Location.getQueryString()) ;
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\"?>");
		sb.append("<params>");
		Map<String, List<String>> parameterMap = Window.Location.getParameterMap() ;
		for (String paramKey : parameterMap.keySet()) {
			List<String> paramValueList = parameterMap.get(paramKey) ;
			String paramValue = paramValueList.get(0) ;
			sb.append("<param>");
			sb.append("<key>");
			sb.append(paramKey);
			sb.append("</key>");
			sb.append("<value>");
			sb.append(paramValue);
			sb.append("</value>");
			sb.append("</param>");
		}
		sb.append("</params>");
		return sb.toString();
	}
	
	private void loadLogout() {
		signOutLayout = buildSignOutLayout() ;
		addMember(signOutLayout);
	}

	private void loadLogin() {
		signInLayout = buildSignInLayout() ;
		addMember(signInLayout);
		
	}

	private Layout buildSignInLayout() {
		VLayout layout = new VLayout();
		Label label = new Label();
		label.setAlign(Alignment.LEFT);
		label.setValign(VerticalAlignment.CENTER);
		label.setBorder("1px solid #808080");
		label.setBackgroundColor("lightblue");
		String contents = "<b>You are working as Anonymous and have ReadOnly Access.</b>";
		contents += "<br><br><br>";
		contents += "<b>Sign In to author new Analyses</b>";
		label.setContents(contents);
		label.setWidth("65%");
		layout.addMember(label);
		Log.debug("Setting login url ==> " + loginInfo.getLoginUrl()) ;
		signInLink.setHref(loginInfo.getLoginUrl());
		layout.addMember(signInLink);
		return layout;
	}

	private Layout buildSignOutLayout() {
		VLayout layout = new VLayout();
		Label label = new Label();
		label.setAlign(Alignment.LEFT);
		label.setValign(VerticalAlignment.CENTER);
		label.setBorder("1px solid #808080");
		label.setBackgroundColor("lightblue");
		String contents = "<b>Welcome ";
		contents += loginInfo.getNickname();
		contents += "</b>";
		label.setContents(contents);
		label.setWidth("65%");
		layout.addMember(label);
		Log.debug("Setting logout url ==> " + loginInfo.getLogoutUrl()) ;
		signOutLink.setHref(loginInfo.getLogoutUrl());
		layout.addMember(signOutLink);
		return layout;
	}

}
